package cn.ydw_03_netty.sendobject.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;
import org.msgpack.type.Value;

import java.util.List;

/**
 * 解码类
 * Created by ydw on 2017/6/28.
 */
public class MsgpackDecoder extends MessageToMessageDecoder<ByteBuf> {
    /**
     *
     * @param channelHandlerContext
     * @param byteBuf 需要解码的byte数组
     * @param list
     * @throws Exception
     */
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        final byte[] array;
        final int length=byteBuf.readableBytes();
        array=new byte[length];
        byteBuf.getBytes(byteBuf.readerIndex(),array,0,length);
        MessagePack messagePack = new MessagePack();
        Value value = messagePack.read(array);//把byte数组序列化为对象
        list.add(value);
    }
}
